package com.aree.caipiao.shsq.chain;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.aree.caipiao.shsq.model.ShuangSeQiuNumbers;
import com.aree.caipiao.shsq.util.ShuangSeQiuUtil;

/**
 * 选号过滤器的基类
 * @author hp-pc
 *
 */
public abstract class ChooseChain {
	protected Log log = LogFactory.getLog(ChooseChain.class);
	
	/**
	 * 在号码池中进行帅选
	 * @param allNumbersIndexes
	 * @return
	 */
	public List<Integer> filter(List<Integer> allNumbersIndexes) {
		log.debug(this.getClass().getName() + "开始过滤");
		if (isEmpty(allNumbersIndexes)) return allNumbersIndexes;
		List<ShuangSeQiuNumbers> allNumbers = getNumbersPool();
		List<Integer> results = new ArrayList<Integer>();
		for (int index : allNumbersIndexes) {
			ShuangSeQiuNumbers numbers = allNumbers.get(index);
			if (isWanted(numbers)) results.add(index);
		}
		log.debug(this.getClass().getName() + "完成过滤");
		if (this.next != null) results = this.next.filter(results);
		return results;
	}
	
	/**
	 * 判断目标组合是不是符合帅选条件
	 * @param allNumbersIndexes
	 * @return
	 */
	protected abstract boolean isWanted(ShuangSeQiuNumbers numbers);
	
	/**
	 * 号码池是否是空的
	 * @param allNumbers
	 * @return
	 */
	protected boolean isEmpty(List<Integer> allNumbers) {
		return allNumbers == null || allNumbers.size() == 0;
	}
	
	private List<ShuangSeQiuNumbers> numbersPool;//号码池
	private ChooseChain next;
	
	public ChooseChain getNext() {
		return next;
	}

	public void setNext(ChooseChain next) {
		this.next = next;
	}

	public List<ShuangSeQiuNumbers> getNumbersPool() {
		if (numbersPool == null) this.numbersPool = ShuangSeQiuUtil.generateAllNumbers();
		return numbersPool;
	}

	public void setNumbersPool(List<ShuangSeQiuNumbers> numbersPool) {
		this.numbersPool = numbersPool;
	}
}
